import {UIRiskSubCategory} from '../entity/UIRiskSubCategory'
import {UIRiskCategory} from '../entity/UIRiskCategory'
import {UIRiskCommonIssue} from '../entity/UIRiskCommonIssue'
import { Injectable } from '@angular/core';
import { Observable } from "rxjs/Observable";
import 'rxjs/add/operator/map';
import { RestResult } from '../entity/rest-result'
import {UIRiskCategoryCriteria} from "../entity/UIRiskCategoryCriteria";
import {PagedResult} from "../entity/PagedResult";
import {UIRiskTaskCriteria} from "../entity/UIRiskTaskCriteria";
import {UIRiskTask} from "../entity/UIRiskTask";
import {UIRiskType} from "../entity/UIRiskType";
import {UIRiskLevel} from "../entity/UIRiskLevel";
import {UIRiskCause} from "../entity/UIRiskCause";
import {UIRiskFixType} from "../entity/UIRiskFixType";
import {UIRiskCheckPlan} from "../entity/UIRiskCheckPlan";
import {UIRiskCheckItemCriteria} from "../entity/UIRiskCheckItemCriteria";
import {UIRiskCheckPlanItem} from "../entity/UIRiskCheckPlanItem";
import {UIRiskReport} from "../entity/UIRiskReport";
import {UIRiskCheckCriteria} from "../entity/UIRiskCheckCriteria";
import {UIRiskCheckTarget} from "../entity/UIRiskCheckTarget";
import {HttpClient} from "@angular/common/http";
import {Recheck_RejectData} from "../entity/Recheck_RejectData";
import {RestRiskInfo} from "../entity/RestRiskInfo";
import {RestRiskCheckTask} from "../entity/RestRiskCheckTask";
import {UIRiskReportCriteria} from "../entity/UIRiskReportCriteria";
import {RestRiskCommonType} from "../entity/RestRiskCommonType";
import {UICCPlatformExpert} from "../entity/UICCPlatformExpert";
import {UIRiskCheckExecution} from "../entity/UIRiskCheckExecution";
import {RestRiskExecution} from "../entity/RestRiskExecution";
import {UICCDimensionCodeAwareTaskCriteria} from "../entity/UICCDimensionCodeAwareTaskCriteria";
import {RestDimensionCodeAwareTask} from "../entity/RestDimensionCodeAwareTask";
import {DOMAIN_SERVER_URL} from "../constants";
import {UICCLawLibrary} from "../entity/UICCLawLibrary";
import {UICCProcessTaskExtension4ExecutionCriteria} from "../entity/UICCProcessTaskExtension4ExecutionCriteria";
import {UICCLocation} from "../entity/UICCLocation";

@Injectable()
export class  CCRiskControllerService {
  constructor (
    private http: HttpClient
  ){ }
  getRiskCategoryList(): Observable<UIRiskCategory[]> {
    return this.http.get<RestResult<UIRiskCategory[]>>(DOMAIN_SERVER_URL + "/windbell/riskCategories" )
      .map((res) => { return res.value })
  }
  getRiskSubCategoryList(riskCategoryId: number): Observable<UIRiskSubCategory[]> {
    return this.http.get<RestResult<UIRiskSubCategory[]>>(DOMAIN_SERVER_URL + "/windbell/riskSubCategories?riskCategoryId=" + riskCategoryId )
      .map((res) => { return res.value })
  }
  getRiskCommonIssueList(riskSubCategoryId: number): Observable<UIRiskCommonIssue[]> {
    return this.http.get<RestResult<UIRiskCommonIssue[]>>(DOMAIN_SERVER_URL + "/windbell/riskCommonIssues?riskSubCategoryId=" + riskSubCategoryId)
      .map((res) => { return res.value })
  }
  createRiskCategory(riskCategory: UIRiskCategory): Observable<UIRiskCategory> {
    return this.http.post<RestResult<UIRiskCategory>>(DOMAIN_SERVER_URL + "/windbell/createRiskCategory" , riskCategory)
      .map((res) => { return res.value })
  }
  createRiskSubCategory(riskCategoryId: number,riskSubCategory: UIRiskSubCategory): Observable<UIRiskSubCategory> {
    return this.http.post<RestResult<UIRiskSubCategory>>(DOMAIN_SERVER_URL + "/windbell/createRiskSubCategory?riskCategoryId=" + riskCategoryId, riskSubCategory)
      .map((res) => { return res.value })
  }
  createRiskCommonIssue(riskSubCategoryId: number,riskCommonIssue: UIRiskCommonIssue): Observable<UIRiskCommonIssue> {
    return this.http.post<RestResult<UIRiskCommonIssue>>(DOMAIN_SERVER_URL + "/windbell/createRiskCommonIssue?riskSubCategoryId=" + riskSubCategoryId , riskCommonIssue)
      .map((res) => { return res.value })
  }
  updateRiskCategory(riskCategory: UIRiskCategory): Observable<UIRiskCategory> {
    return this.http.post<RestResult<UIRiskCategory>>(DOMAIN_SERVER_URL + "/windbell/updateRiskCategory" , riskCategory)
      .map((res) => { return res.value })
  }
  updateRiskSubCategory(riskSubCategory: UIRiskSubCategory): Observable<UIRiskSubCategory> {
    return this.http.post<RestResult<UIRiskSubCategory>>(DOMAIN_SERVER_URL + "/windbell/updateRiskSubCategory" , riskSubCategory)
      .map((res) => { return res.value })
  }
  updateCommonIssue(riskCommonIssue: UIRiskCommonIssue): Observable<UIRiskCommonIssue> {
    return this.http.post<RestResult<UIRiskCommonIssue>>(DOMAIN_SERVER_URL + "/windbell/updateCommonIssue" , riskCommonIssue)
      .map((res) => { return res.value })
  }
  deleteRiskCategory(riskCategoryId: number): Observable<any> {
    return this.http.get<RestResult<any>>(DOMAIN_SERVER_URL + "/windbell/deleteRiskCategory?riskCategoryId=" + riskCategoryId)
      .map((res) => { return res.value })
  }
  deleteRiskSubCategory(riskSubCategoryId: number): Observable<any> {
    return this.http.get<RestResult<any>>(DOMAIN_SERVER_URL + "/windbell/deleteRiskSubCategory?riskSubCategoryId=" + riskSubCategoryId )
      .map((res) => { return res.value })
  }
  deleteRiskCommonIssue(riskCommonIssueId: number): Observable<any> {
    return this.http.get<RestResult<any>>(DOMAIN_SERVER_URL + "/windbell/deleteRiskCommonIssue?riskCommonIssueId=" + riskCommonIssueId )
      .map((res) => { return res.value })
  }

  getRiskCategoryByCriteriaList(criteria: UIRiskCategoryCriteria): Observable<PagedResult<UIRiskCategory>> {
    return this.http.post<RestResult<PagedResult<UIRiskCategory>>>(DOMAIN_SERVER_URL + "/windbell/query/riskCategories" , criteria)
        .map((res) => { return res.value })
  }

  getRiskTaskByCriteriaList(criteria: UIRiskTaskCriteria): Observable<PagedResult<UIRiskTask>> {
    console.log('sending request getRiskTaskByCriteriaList...');
    return this.http.post<RestResult<PagedResult<UIRiskTask>>>(DOMAIN_SERVER_URL + "/windbell/riskcheck/list" , criteria).map((res) => { return res.value })
  }

  riskCheckItemList(uiCriteria: UIRiskCheckItemCriteria): Observable<any /*TODO: specifiy type argument*/> {
    return this.http.post<RestResult<any>>(DOMAIN_SERVER_URL + "/windbell/riskCheckItem/list" , uiCriteria).map((res) => { return res.value })
  }
  getRiskType(): Observable<UIRiskType[]> {
    return this.http.get<RestResult<UIRiskType[]>>(DOMAIN_SERVER_URL + "/windbell/riskType" ).map((res) => { return res.value })
  }
  getRiskLevel(): Observable<UIRiskLevel[]> {
    return this.http.get<RestResult<UIRiskLevel[]>>(DOMAIN_SERVER_URL + "/windbell/riskLevel" ).map((res) => { return res.value })
  }
  getRiskCause(): Observable<UIRiskCause[]> {
    return this.http.get<RestResult<UIRiskCause[]>>(DOMAIN_SERVER_URL + "/windbell/riskCause" ).map((res) => { return res.value })
  }
  getRiskFixType(): Observable<UIRiskFixType[]> {
    return this.http.get<RestResult<UIRiskFixType[]>>(DOMAIN_SERVER_URL + "/windbell/riskFixType" ).map((res) => { return res.value })
  }

  checkTargetList(): Observable<UIRiskCheckTarget[]> {
    return this.http.get<RestResult<UIRiskCheckTarget[]>>(DOMAIN_SERVER_URL + "/windbell/checkTarget" ).map((res) => { return res.value })
  }

  riskCheckStatusList(): Observable<string[]> {
    return this.http.get<RestResult<string[]>>(DOMAIN_SERVER_URL + "/windbell/riskCheckStatus" ).map((res) => { return res.value })
  }
  riskCheckItemStatusList(): Observable<string[]> {
    return this.http.get<RestResult<string[]>>(DOMAIN_SERVER_URL + "/windbell/riskCheckItemStatus" ).map((res) => { return res.value })
  }

  uploadFile(file: File): Observable<RestResult<string>> {
    const formData: any = new FormData();
    formData.append('file', file, file.name);
    return this.http.post<RestResult<string>>(DOMAIN_SERVER_URL + "/api/upload" , formData)
  }

  // -------------------------------------------------------------------------------------------------------------------

  riskCheckListByCriteria(uiCriteria: UIRiskCheckCriteria): Observable<PagedResult<UIRiskCheckPlan>> {
    return this.http.post<RestResult<PagedResult<UIRiskCheckPlan>>>(DOMAIN_SERVER_URL + "/windbell/riskCheck/list" , uiCriteria).map((res)=>{
      return res.value
    })
  }


  // -------------------------------------------------------------------------------------------------------------------

  // 任务信息相关 start------------------------

  taskList(uiCriteria: UIRiskTaskCriteria): Observable<PagedResult<RestRiskCheckTask>> {
    return this.http.post<RestResult<PagedResult<RestRiskCheckTask>>>(DOMAIN_SERVER_URL + "/windbell/risk/taskList" , uiCriteria).map((res)=>{
      return res.value
    })
  }

  finishedExecutionList(uiCriteria: UIRiskTaskCriteria): Observable<UIRiskCheckExecution[]> {
    return this.http.post<RestResult<UIRiskCheckExecution[]>>(DOMAIN_SERVER_URL + "/windbell/riskCheckExecution/finishedlist" , uiCriteria).map((res)=>{
      return res.value
    })
  }

  executionList(uiCriteria: UICCProcessTaskExtension4ExecutionCriteria): Observable<PagedResult<UIRiskCheckExecution>> {
    return this.http.post<RestResult<PagedResult<UIRiskCheckExecution>>>(DOMAIN_SERVER_URL + "/windbell/risk/checkExecution/list" , uiCriteria).map((res)=>{
      return res.value
    })
  }

  dimensionTaskList(uiCriteria: UICCDimensionCodeAwareTaskCriteria): Observable<PagedResult<RestDimensionCodeAwareTask>> {
    return this.http.post(DOMAIN_SERVER_URL + "/windbell/risk/qrTaskList" , uiCriteria).map((res)=>{
      let restResult = res as RestResult<PagedResult<RestDimensionCodeAwareTask>>
      return restResult.value
    })
  }

  getTaskDetail(taskId: number): Observable<RestRiskCheckTask> {
    return this.http.get<RestResult<RestRiskCheckTask>>(DOMAIN_SERVER_URL + `/windbell/risk/${taskId}` ).map((res)=>{
      return res.value
    })
  }

  getTaskTypeList(): Observable<any> {
    return this.http.get(DOMAIN_SERVER_URL + `/windbell/risk/taskType` ).map((res)=>{
      let restResult = res as RestResult<any>
      return restResult.value
    })
  }

  getSubCategory(subCategoryId: number): Observable<UIRiskSubCategory> {
    return this.http.get<RestResult<UIRiskSubCategory>>(DOMAIN_SERVER_URL + `/windbell/risk/subcategory/${subCategoryId}` ).map((res)=>{
      return res.value
    })
  }

  getCategory(taskId: number): Observable<UIRiskCategory> {
    return this.http.get<RestResult<UIRiskCategory>>(DOMAIN_SERVER_URL + `/windbell/risk/${taskId}/category` ).map((res)=>{
      return res.value
    })
  }

  riskCheckExecutionDetail(id: number): Observable<RestRiskExecution> {
    return this.http.get(DOMAIN_SERVER_URL + `/windbell/risk/riskCheckExecution/${id}` ).map((res)=>{
      let restResult = res as RestResult<RestRiskExecution>
      return restResult.value
    })
  }

  // getRiskSubCategory(id: number): Observable<UIRiskSubCategory> {
  //   return this.http.get(DOMAIN_SERVER_URL + `/windbell/risk/execution/${id}/subcategory` ).map((res)=>{
  //     let restResult = res as RestResult<UIRiskSubCategory>
  //     return restResult.value
  //   })
  // }

  getRiskSubCategory(id: number): Observable<UIRiskCategory> {
    return this.http.get(DOMAIN_SERVER_URL + `/windbell/risk/execution/${id}/category` ).map((res)=>{
      let restResult = res as RestResult<UIRiskCategory>
      return restResult.value
    })
  }

  // 任务流程相关 start------------------------

  withoutRisk(taskId: number, riskInfo: RestRiskInfo): Observable<any> {
    return this.http.post<RestResult<any>>(DOMAIN_SERVER_URL + `/windbell/risk/${taskId}/norisk` , riskInfo).map((res)=>{
      return res.value
    })
  }
  withRisk(taskId: number,riskInfo: RestRiskInfo): Observable<any /*TODO add explicit return type*/> {
    return this.http.post(DOMAIN_SERVER_URL + `/windbell/risk/${taskId}/withrisk` , riskInfo).map((res)=>{
      let restResult = res as RestResult<any /*TODO add explicit return type*/>
      return restResult.value
    })
  }

  startCheckCompleted(taskId: number, riskInfo: RestRiskInfo): Observable<any /*TODO add explicit return type*/> {
    return this.http.post(DOMAIN_SERVER_URL + `/windbell/risk/${taskId}/startCheckCompleted` , riskInfo).map((res)=>{
      let restResult = res as RestResult<any /*TODO add explicit return type*/>
      return restResult.value
    })
  }
  recheckApprove(taskId: number, riskInfo: RestRiskInfo): Observable<any /*TODO add explicit return type*/> {
    return this.http.post(DOMAIN_SERVER_URL + `/windbell/risk/${taskId}/recheckApprove` , riskInfo).map((res)=>{
      let restResult = res as RestResult<any /*TODO add explicit return type*/>
      return restResult.value
    })
  }
  recheckReject(taskId: number, data: Recheck_RejectData): Observable<any /*TODO add explicit return type*/> {
    return this.http.post(DOMAIN_SERVER_URL + `/windbell/risk/${taskId}/recheckReject` , data).map((res)=>{
      let restResult = res as RestResult<any /*TODO add explicit return type*/>
      return restResult.value
    })
  }

  // 隐患报告相关 start------------------------

  riskReportList(): Observable<any /*TODO: specifiy type argument*/> {
    return this.http.get(DOMAIN_SERVER_URL + "/windbell/riskReport/list" ).map((res)=>{
      let restResult = res as RestResult<any /*TODO: specifiy type argument*/>
      return restResult.value
    })
  }
  getRiskReportList(uiCriteria: UIRiskReportCriteria): Observable<PagedResult<UIRiskReport>> {
    return this.http.post(DOMAIN_SERVER_URL + "/windbell/risk/reportList" , uiCriteria).map((res)=>{
      let restResult = res as RestResult<PagedResult<UIRiskReport>>
      return restResult.value
    })
  }

  getRiskReportDetail(id: number): Observable<UIRiskReport> {
    return this.http.get(DOMAIN_SERVER_URL + `/windbell/risk/report/${id}/detail` ).map((res)=>{
      let restResult = res as RestResult<UIRiskReport>
      return restResult.value
    })
  }
  getRiskCommonType(): Observable<RestRiskCommonType> {
    return this.http.get(DOMAIN_SERVER_URL + "/windbell/risk/commontype" ).map((res)=>{
      let restResult = res as RestResult<RestRiskCommonType>
      return restResult.value
    })
  }

  createRiskReportByTaskId(id: number,report: UIRiskReport): Observable<any /*TODO: specifiy type argument*/> {
    return this.http.post(DOMAIN_SERVER_URL + `/windbell/risk/task/${id}/report` , report).map((res)=>{
      let restResult = res as RestResult<any /*TODO: specifiy type argument*/>
      return restResult.value
    })
  }
  createRiskReportByExecutionId(id: number,report: UIRiskReport): Observable<any /*TODO: specifiy type argument*/> {
    return this.http.post(DOMAIN_SERVER_URL + `/windbell/risk/execution/${id}/report` , report).map((res)=>{
      let restResult = res as RestResult<any /*TODO: specifiy type argument*/>
      return restResult.value
    })
  }
  updateRiskReport(report: UIRiskReport): Observable<any /*TODO: specifiy type argument*/> {
    return this.http.post(DOMAIN_SERVER_URL + "/windbell/risk/report" , report).map((res)=>{
      let restResult = res as RestResult<any /*TODO: specifiy type argument*/>
      return restResult.value
    })
  }
  deleteRiskReport(id: number): Observable<any /*TODO add explicit return type*/> {
    return this.http.get(DOMAIN_SERVER_URL + `/windbell/risk/report/${id}` ).map((res)=>{
      let restResult = res as RestResult<any /*TODO add explicit return type*/>
      return restResult.value
    })
  }

  // admin start

  getUserInfo(id: number): Observable<UICCPlatformExpert> {
    return this.http.get(DOMAIN_SERVER_URL + `/windbell/user/${id}` ).map((res)=>{
      let restResult = res as RestResult<UICCPlatformExpert>
      return restResult.value
    })
  }

  riskCheckList(uiCriteria: UIRiskCheckCriteria): Observable<any /*TODO: specifiy type argument*/> {
    return this.http.post(DOMAIN_SERVER_URL + "/windbell/riskCheck/list" , uiCriteria).map((res)=>{
      let restResult = res as RestResult<any /*TODO: specifiy type argument*/>
      return restResult.value
    })
  }
  getRiskCheckDetailById(id: number): Observable<UIRiskCheckPlan> {
    return this.http.get(DOMAIN_SERVER_URL + `/windbell/riskCheck/${id}` ).map((res)=>{
      let restResult = res as RestResult<UIRiskCheckPlan>
      return restResult.value
    })
  }
  getRiskCheckItemDetail(id: number): Observable<UIRiskCheckPlanItem> {
    return this.http.get(DOMAIN_SERVER_URL + `/windbell/riskCheckItem/${id}` ).map((res)=>{
      let restResult = res as RestResult<UIRiskCheckPlanItem>
      return restResult.value
    })
  }

  riskCheckExecutionList4Plan(id: number): Observable<UIRiskCheckExecution[]> {
    return this.http.get(DOMAIN_SERVER_URL + `/windbell/risk/${id}/riskCheckExecution/list` ).map((res)=>{
      let restResult = res as RestResult<UIRiskCheckExecution[]>
      return restResult.value;
    });
  }

  lawLibraryList(): Observable<UICCLawLibrary[]> {
    return this.http.get(DOMAIN_SERVER_URL + "/windbell/lawLibraryList" ).map((res)=>{
      let restResult = res as RestResult<UICCLawLibrary[]>
      return restResult.value;
    });
  }

  locationList(id: number): Observable<UICCLocation[]> {
    return this.http.get(DOMAIN_SERVER_URL + `/windbell/risk/task/${id}/locations` ).map((res)=>{
      let restResult = res as RestResult<UICCLocation[]>
      return restResult.value;
    });
  }


}
